<?php
class information extends CI_Model
{
    public $id;
    public $ticket_number;
    public $ticket_title;
    public $check_in;
    public $created_date;

    public function get_elements()
    {
        $query = $this->db->query("SELECT * FROM information");
        return $query->result();
    }

    public function get_elements_checked_in()
    {
        $query = $this->db->query("SELECT * FROM information WHERE check_in IS NOT NULL AND check_in <> 0");
        return $query->result();
    }

    public function get_summary_info(){
        $query = $this->db->query("SELECT ticket_title,COUNT(*) AS sum_number FROM information WHERE check_in <> '0' AND check_in IS NOT NULL GROUP BY ticket_title");
        return $query->result();
    }

    public function get_check_in_count($ticket_title){
        $query = $this->db->query("SELECT COUNT(*) AS cnt FROM information WHERE ticket_title = ? AND check_in <> '0' AND check_in IS NOT NULL",array($ticket_title));
        return $query->row();
    }

    public function get_all_ticket_type(){
        $query = $this->db->query("SELECT * FROM information GROUP BY ticket_title");
        return $query->result();
    }

    public function get_element_by_id($id)
    {
        $query = $this->db->query("SELECT * FROM information WHERE id = ?",array($id));
        return $query->row();
    }

    public function get_element_by_ticket_number($id)
    {
        $query = $this->db->query("SELECT * FROM information WHERE ticket_number = ?",array($id));
        return $this->to_domain_model($query->row());
    }

    public function insert_new_ticket($data){
        $insert_id = -1;
        $this->db->insert('information',$data);
        $insert_id = $this->db->insert_id();
        if($insert_id != -1){
            return true;
        }
        return false;
    }

    public function update_ticket($data){
        $this->db->where('id',$data['id']);
        $this->db->update('information',$data);
        return ($this->db->affected_rows() > 0) ? TRUE : FALSE;
    }

    public function to_domain_model($item)
    {
        $model = new information();
        $model->id = isset($item->id) ? $item->id : '' ;
        $model->ticket_number = isset($item->ticket_number) ? $item->ticket_number : '' ;
        $model->ticket_title = isset($item->ticket_title) ? $item->ticket_title : '' ;
        $model->check_in = isset($item->check_in) ? $item->check_in : '' ;
        $model->created_date = isset($item->created_date) ? $item->created_date : '' ;
        return $model;
    }
}